/**
 * 
 * @param langMask
 *            masque représentant les langues (1:en, 2:fr, 4:autres)
 */
function getHTML_flags(langMask) {
    $flags = '';
    if (langMask == 0)
        return '-';
    if ((langMask & 1) == 1)
        $flags += "<img class='flag' src='/mediaglobe/images/icons/flag_en.jpg'/>";
    if ((langMask & 2) == 2)
        $flags += "<img class='flag' src='/mediaglobe/images/icons/flag_fr.jpg'/>";
    if ((langMask & 4) == 4)
        $flags += "<img class='flag' src='/mediaglobe/images/icons/flag_other.jpg'/>";
    return $flags;
}

function getMovieFormat(formatCode) {
    if (formatCode == 1)
        return 'Blue-ray';
    if (formatCode == 2)
        return 'DVD';
    if (formatCode == 3)
        return 'Virtuel';
    return '-';
}

function getHTML_rating_img(value, title) {
    var url = "/mediaglobe/images/icons/star.png";
    html = "<img src='" + url + "' data-index='" + value + "' title='" + title + "'/>";
    return html;
}

function getHTML_rating_editable(movie, index, field) {
    
    var value = movie[field];
    if (value == null)
        value = 0;
    
    var html = "";
    html += "<div id='rating-" + index + "' class='rating rating-editable'";
    html += " data-index='" + index + "'";
    html += " data-rating='" + value + "'";
    html += " data-idmovie='" + movie['idMovie'] + "'";
    html += ">";
    html += "<div id='bg-" + index + "' class='rating-bg'></div>";
//    html += "<img src='star.png' data-index='1' title='Nul !'/><img src='star.png' data-index='2' title='Moyen...'/><img src='star.png' data-index='3' title='Bon film'/><img src='star.png' data-index='4' title='Excellent !'/>";
    html += getHTML_rating_img(1, 'Nul !');
    html += getHTML_rating_img(2, 'Moyen...');
    html += getHTML_rating_img(3, 'Bon');
    html += getHTML_rating_img(4, 'Excellent !');
    html += "</div>";
    return html;
}

function getHTML_rating(movie, field) {
    var value = movie[field];
    if (value == null)
        value = 0;

    var title = 'Note moyenne des utilisateurs:' + movie[field];

    var width = value * 16.0;
    
    var html_img_star = "<img src='/mediaglobe/images/icons/star.png'/>";
    var html = "";
    html += "<div class='rating' title='" + title + "'>";
    html += "<div class='rating-bg' style='width:" + width + "px'></div>";
//    html += "<img src='star.png'/><img src='star.png'/><img src='star.png'/><img src='star.png'/>";
    html += html_img_star;
    html += html_img_star;
    html += html_img_star;
    html += html_img_star;
    html += "</div>";
    return html;
}

function getHTML_tr_header(currentUser, selectedUser, translations) {
    var html = "";
    
    // console.debug('%o %o %o', currentUser, selectedUser, translations);
    
    html += "<tr>";
    html += "<th>" + translations.name_orig + "</th>";
    html += "<th>" + translations.name_fr + "</th>";
    html += "<th>" + translations.release + "</th>";
    if (currentUser == selectedUser)
        html += "<th>" + translations.my_rating + "</th>";
    else
        html += "<th>" + translations.his_rating + "</th>";
    html += "<th>" + translations.avg_rating + "</th>";
    html += "<th>" + translations.format + "</th>";
    html += "<th>" + translations.quality + "</th>";
    html += "<th>" + translations.languages + "</th>";
    html += "<th>" + translations.subtitles + "</th>";
    if (currentUser == selectedUser) {
        html += "<th>" + translations.watched + "</th>";
        html += "<th>" + translations.remove + "</th>";
    } else {
        html += "<th>Demander</th>";
        html += "<th>Ajouter à ma liste</th>";
    }
    html += "</tr>";
    
    return html;
}

function getHTML_tr_movie(currentUser, selectedUser, movies, index) {
    var html = "";
    
//    console.debug('%o %o %o %o', currentUser, selectedUser, movies, index);
    
    html += "<tr>";
    html += "<td>" + movies[index].name + "</td>";
    html += "<td>" + movies[index].nameFr + "</td>";
    html += "<td>" + movies[index].dateOut + "</td>";
    if (currentUser == selectedUser)
        html += "<td>" + getHTML_rating_editable(movies[index], index, 'myRating') + "</td>"; // movies[index].myRating
    else
        html += "<td>" + getHTML_rating(movies[index], 'friendRating') + "</td>"; // movies[index].friendRating
    html += "<td>" + getHTML_rating(movies[index], 'avgRating') + "</td>"; // movies[index].avgRating
    html += "<td>" + movies[index].format + "</td>";
    html += "<td>" + movies[index].quality + "</td>";
    html += "<td>" + getHTML_flags(movies[index].languages) + "</td>";
    html += "<td>" + getHTML_flags(movies[index].subtitles) + "</td>";
    
    if (currentUser == selectedUser) {
        html += "<td>FIXME</td>";
        html += "<td><img class='button' src='/mediaglobe/images/icons/delete.png'/></td>";
    } else {
        if (movies[index].owned == 'yes') {
            html += "<td><label title='Ce film est déjà dans votre médiathèque'>-</label></td>";
            html += "<td><label title='Ce film est déjà dans votre médiathèque'>-</label></td>";
        } else if (movies[index].asked == 'yes') {
            html += "<td><img class='button' title='Annuler cette demande de partage' src='/mediaglobe/images/icons/cancel.png'/></td>";
            html += "<td><img class='button' title='Ajouter à ma liste de film' src='/mediaglobe/images/icons/add.png'/></td>";
        } else {
            html += "<td><img class='button' title='Faire une demande de partage' src='/mediaglobe/images/icons/ask.png'/></td>";
            html += "<td><img class='button' title='Ajouter à ma liste de film' src='/mediaglobe/images/icons/add.png'/></td>";
        }
    }
    
    html += "</tr>";
    return html;
}

/**
 * recupere les films d'un utilisateur (soi-meme ou un ami) puis les affiches
 * dans un tableau.
 * 
 * @param idUser
 *            id utilisateur
 */
function getMovies(idUser) {
    
    $('#movies-content').empty();
    
    $.ajax({
        type : "GET",
        url : readCookie("baseurl") + "/library/getmovies/u/" + idUser,
        success : function(json) {
            
            if (!json.success) {
                alert("Une erreur est survenue (js:getMovies)");
                return;
            }
            
            var html = "";
            html += "<table>";
            html += getHTML_tr_header(idUser, json.idUser, json.translations);
            for ( var i = 0; i < json.count; ++i) {
                html += getHTML_tr_movie(idUser, json.idUser, json.movies, i);
            }
            html += "</table>";
            
            $('#movies-content').append(html);
            
            init_rating();
            
            // TODO: Export
            // var exportButton = "<input title='Faire un export de cette liste
            // de film' type='button' value='Exporter la liste'/>";
            // $('#movies-content').append(exportButton);
        }
    });
    
}
